﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Cav;
using Cav.WinForm.BaseClases;

namespace Nefela.UI
{
    public partial class fmMainForm : Form
    {
        public fmMainForm()
        {
            InitializeComponent();

            setBL.LoadSetting();


            if (!Properties.Settings.Default.OutputDir.IsNullOrWhiteSpace())
            {
                setBL.ModelPackegeDocs.OutputDir = Properties.Settings.Default.OutputDir;
                setBL.Save();

                setBL.LoadSetting();

                Properties.Settings.Default.OutputDir = null;
                Properties.Settings.Default.Save();
            }
        }

        private void tsmiClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void stmiSetDoc_Click(object sender, EventArgs e)
        {
            fmSettings fs = new fmSettings();
            fs.BusinessLogic = setBL;
            fs.ShowDialog(this);
        }

        private void ExportSets_Click(object sender, EventArgs e)
        {
            var fn = Dialogs.SaveFile(
               Title: "Экспорт настроек",
               Filter: "Настройки Нефела|*.nefelaconfig",
               SupportMultiDottedExtensions: true);

            if (fn.IsNullOrWhiteSpace())
                return;
            try
            {
                if (File.Exists(fn))
                    File.Delete(fn);

                setBL.ExportDocs(fn);
            }
            catch (Exception ex)
            {
                Dialogs.ErrorF(this, ex.Expand());
            }
        }

        private void tsmiImportSets_Click(object sender, EventArgs e)
        {
            var fn = Dialogs.FileBrowser(
            Title: "Импорт настроек",
            SupportMultiDottedExtensions: true,
            Filter: "Настройки Нефела|*.nefelaconfig");

            var fil = fn.FirstOrDefault();

            if (fil.IsNullOrWhiteSpace())
                return;

            if (!File.Exists(fil))
            {
                Dialogs.InformationF(this, "Указанного файла не существует");
                return;
            }

            try
            {
                if (!Dialogs.QuestionOKCancelF(this, "При импорте текущие настройки документов будут утеряны. Продолжить?"))
                    return;

                setBL.ImportDocs(fil);
            }
            catch (Exception ex)
            {
                Dialogs.ErrorF(this, ex.Expand());
            }
        }

        private void fmMainForm_Load(object sender, EventArgs e)
        {
            setBL.LoadSetting();

            tsmiExportSets.Enabled =
                setBL.ModelPackegeDocs.CardCustomer.Count > 0 &&
                setBL.ModelPackegeDocs.TemplatesDocs.Count > 0;

            foreach (Control i in tabControl1.TabPages.Cast<TabPage>().SelectMany(x => x.Controls.Cast<Control>()))
                if (i is UserControlBase)
                    ((UserControlBase)i).RefreshControl();
        }

        private void nefelacodeplexcomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("http://nefela.codeplex.com");
        }

        private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (e.Action != TabControlAction.Selecting)
                return;
            fmMainForm_Load(null, null);
        }

        private void документацияToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("https://nefela.codeplex.com/documentation");
        }

        private void fmMainForm_HelpRequested(object sender, HelpEventArgs hlpevent)
        {
            Process.Start("https://goo.gl/of6oeH");
        }

        private void tsmiFeedBack_Click(object sender, EventArgs e)
        {
            Process.Start("https://goo.gl/t3ewhb");            
        }

    }
}
